import type { IUserLoginData } from '@/api/user'
import { login as loginApi, loginMock } from '@/api/user'
import { removeToken, setToken } from '@/utils/auth'
import { useTagsViewStore } from './tagsView'
export const useUserStore = defineStore('user', () => {
  const tagsViewStore = useTagsViewStore()
  const state = reactive({
    token: ''
  })
  const login = async (userInfo: IUserLoginData) => {
    try {
      const { username, password } = userInfo
      // const response = await loginApi({
      const response = await loginMock({
        username: username.trim(),
        password: password.trim()
      })
      const { data } = response
      state.token = data.token
      setToken(data.token)
    } catch (err) {
      return Promise.reject(err)
    }
  }
  const logout = () => {
    state.token = ''
    removeToken()
    tagsViewStore.delAllView()
  }

  return { state, login, logout }
})
